package com.yida.gwj.basis.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.yida.common.core.exception.ServiceException;
import com.yida.common.core.utils.DateUtils;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.security.utils.SecurityUtils;
import com.yida.gwj.basis.domain.GwjOwnerAttest;
import com.yida.gwj.basis.domain.dto.GwjOwnerAttestDetailDTO;
import com.yida.gwj.basis.domain.request.GwjOwnerAttestApproveRequest;
import com.yida.gwj.basis.domain.request.GwjOwnerAttestRequest;
import com.yida.gwj.basis.enums.GwjOwnerAttestApproveStatus;
import com.yida.gwj.basis.mapper.GwjOwnerAttestMapper;
import com.yida.gwj.basis.service.IGwjOwnerAttestService;
import com.yida.gwj.common.constant.GwjConstants;
import com.yida.gwj.common.domain.GwjBaseHouse;
import com.yida.gwj.common.domain.GwjBaseOwner;
import com.yida.gwj.common.service.IGwjBaseBuildService;
import com.yida.gwj.common.service.IGwjBaseCommunityService;
import com.yida.gwj.common.service.IGwjBaseHouseService;
import com.yida.gwj.common.service.IGwjBaseOwnerService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 业主信息认证Service业务层处理
 *
 * @author lqq
 * @date 2023-08-26
 */
@Service
public class GwjOwnerAttestServiceImpl implements IGwjOwnerAttestService {

    @Resource
    private GwjOwnerAttestMapper gwjOwnerAttestMapper;

    @Resource
    private IGwjBaseOwnerService gwjBaseOwnerService;

    @Resource
    private IGwjBaseCommunityService gwjBaseCommunityService;

    @Resource
    private IGwjBaseHouseService gwjBaseHouseService;

    @Resource
    private IGwjBaseBuildService gwjBaseBuildService;

    /**
     * 查询业主信息认证
     *
     * @param ownerAttestId 业主信息认证主键
     * @return 业主信息认证
     */
    @Override
    public GwjOwnerAttest selectGwjOwnerAttestByOwnerAttestId(Long ownerAttestId) {
        return gwjOwnerAttestMapper.selectGwjOwnerAttestByOwnerAttestId(ownerAttestId);
    }

    /**
     * 查询业主信息认证列表
     *
     * @param gwjOwnerAttest 业主信息认证
     * @return 业主信息认证
     */
    @Override
    public List<GwjOwnerAttest> selectGwjOwnerAttestList(GwjOwnerAttest gwjOwnerAttest) {
        return gwjOwnerAttestMapper.selectGwjOwnerAttestList(gwjOwnerAttest);
    }

    /**
     * 新增业主信息认证
     *
     * @param gwjOwnerAttest 业主信息认证
     * @return 结果
     */
    @Override
    public int insertGwjOwnerAttest(GwjOwnerAttest gwjOwnerAttest) {
        gwjOwnerAttest.setCreateTime(DateUtils.getNowDate());
        return gwjOwnerAttestMapper.insertGwjOwnerAttest(gwjOwnerAttest);
    }

    /**
     * 修改业主信息认证
     *
     * @param gwjOwnerAttest 业主信息认证
     * @return 结果
     */
    @Override
    public int updateGwjOwnerAttest(GwjOwnerAttest gwjOwnerAttest) {
        gwjOwnerAttest.setUpdateTime(DateUtils.getNowDate());
        return gwjOwnerAttestMapper.updateGwjOwnerAttest(gwjOwnerAttest);
    }

    /**
     * 批量删除业主信息认证
     *
     * @param ownerAttestIds 需要删除的业主信息认证主键
     * @return 结果
     */
    @Override
    public int deleteGwjOwnerAttestByOwnerAttestIds(Long[] ownerAttestIds) {
        return gwjOwnerAttestMapper.deleteGwjOwnerAttestByOwnerAttestIds(ownerAttestIds);
    }

    /**
     * 删除业主信息认证信息
     *
     * @param ownerAttestId 业主信息认证主键
     * @return 结果
     */
    @Override
    public int deleteGwjOwnerAttestByOwnerAttestId(Long ownerAttestId) {
        return gwjOwnerAttestMapper.deleteGwjOwnerAttestByOwnerAttestId(ownerAttestId);
    }

    /**
     * 提交认证资料
     *
     * @param request 业主信息认证
     * @return 结果
     */
    @Override
    public int submit(GwjOwnerAttestRequest request) {
        GwjOwnerAttest gwjOwnerAttest = BeanUtil.copyProperties(request, GwjOwnerAttest.class);
        gwjOwnerAttest.setApplicantTime(DateUtils.getNowDate());
        gwjOwnerAttest.setStatus(GwjOwnerAttestApproveStatus.NOT_APPROVE.getCode());
        gwjOwnerAttest.setCreateBy(request.getOwnerName());
        //查询旧数据
        GwjBaseHouse baseHouse = gwjBaseHouseService.selectGwjBaseHouseByGwjBaseHouseId(request.getHouseId());
        GwjOwnerAttestDetailDTO oldOwnerInfo = new GwjOwnerAttestDetailDTO() {{
            setBuildName(request.getBuildName());
            setCommunityName(request.getCommunityName());
            setRoomNum(request.getRoomNum());
            setCertificateAddress(baseHouse.getHouseLocation());
            setCertificateNum(baseHouse.getCertificateNum());
        }};
        GwjBaseOwner baseOwner = gwjBaseOwnerService.selectGwjBaseOwnerByAreaIdAndHouseCode(request.getHouseCode(), request.getAreaId());
        if (StringUtils.isNotNull(baseOwner)) {
            oldOwnerInfo.setOwnerName(baseOwner.getOwnerName());
            oldOwnerInfo.setOwnerPhone(baseOwner.getOwnerPhone());
            oldOwnerInfo.setCardNum(baseOwner.getCardNum());
        }
        gwjOwnerAttest.setOldOwnerInfo(JSONUtil.toJsonStr(JSONUtil.parseObj(oldOwnerInfo, false)));
        return this.insertGwjOwnerAttest(gwjOwnerAttest);
    }

    /**
     * 审批通过
     *
     * @param request /
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void approve(GwjOwnerAttestApproveRequest request) {
        GwjOwnerAttest gwjOwnerAttest = this.selectGwjOwnerAttestByOwnerAttestId(request.getOwnerAttestId());
        if (StringUtils.isNull(gwjOwnerAttest)) {
            throw new ServiceException("审批失败,查无业主认证申请资料");
        }
        if (!StringUtils.equals(gwjOwnerAttest.getStatus(), GwjOwnerAttestApproveStatus.NOT_APPROVE.getCode())) {
            throw new ServiceException("审批失败,无法重复审核");
        }
        if (request.getOutsideFlag()) {
            gwjOwnerAttest.setApproveUserName(SecurityUtils.getLoginUser().getSysOutsideUser().getRealName());
            gwjOwnerAttest.setApproveUserId(SecurityUtils.getLoginUser().getSysOutsideUser().getUserId());
            gwjOwnerAttest.setUpdateBy(SecurityUtils.getLoginUser().getSysOutsideUser().getRealName());
        } else {
            gwjOwnerAttest.setApproveUserName(SecurityUtils.getLoginUser().getSysUser().getRealName());
            gwjOwnerAttest.setApproveUserId(SecurityUtils.getLoginUser().getSysUser().getUserId());
            gwjOwnerAttest.setUpdateBy(SecurityUtils.getLoginUser().getSysUser().getRealName());
        }
        gwjOwnerAttest.setStatus(request.getApproveCode());
        gwjOwnerAttest.setApproveContent(request.getApproveContent());
        gwjOwnerAttest.setApproveTime(DateUtils.getNowDate());
        if (this.updateGwjOwnerAttest(gwjOwnerAttest) != 1) {
            throw new ServiceException("审批失败,更新业主认证申请资料失败");
        }
        //审批通过，更新业主数据
        if (StringUtils.equals(request.getApproveCode(), GwjOwnerAttestApproveStatus.PASS.getCode())) {
            //更新或新增业主数据
            GwjBaseOwner baseOwner = gwjBaseOwnerService.selectGwjBaseOwnerByAreaIdAndHouseCode(gwjOwnerAttest.getHouseCode(), gwjOwnerAttest.getAreaId());
            //新增
            if (StringUtils.isNull(baseOwner)) {
                GwjBaseOwner newOwner = new GwjBaseOwner() {{
                    setImportFlag(GwjConstants.NO);
                    setAreaId(gwjOwnerAttest.getAreaId());
                    setStreetId(gwjOwnerAttest.getStreetId());
                    setHouseCode(gwjOwnerAttest.getHouseCode());
                    setCardNum(gwjOwnerAttest.getCardNum());
                    setOwnerName(gwjOwnerAttest.getOwnerName());
                    setOwnerPhone(gwjOwnerAttest.getOwnerPhone());
                }};
                if (gwjBaseOwnerService.insertGwjBaseOwner(newOwner) != 1) {
                    throw new ServiceException("审批失败,新增基础业主信息失败");
                }
            }
            //更新
            else {
                baseOwner.setCardNum(gwjOwnerAttest.getCardNum());
                baseOwner.setOwnerName(gwjOwnerAttest.getOwnerName());
                baseOwner.setOwnerPhone(gwjOwnerAttest.getOwnerPhone());
                if (gwjBaseOwnerService.updateGwjBaseOwner(baseOwner) != 1) {
                    throw new ServiceException("审批失败,更新基础业主信息失败");
                }
            }
            //更新基础房屋产权证号
            GwjBaseHouse baseHouse = gwjBaseHouseService.selectGwjBaseHouseByGwjBaseHouseId(gwjOwnerAttest.getHouseId());
            baseHouse.setCertificateNum(gwjOwnerAttest.getCertificateNum());
            if (gwjBaseHouseService.updateGwjBaseHouse(baseHouse) != 1) {
                throw new ServiceException("审批失败,更新基础房屋信息失败");
            }
        }
    }
}
